package Task4;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        Socket s = null;
        ObjectOutputStream oos = null;
        UserMessage um = null;
        ObjectInputStream ois = null;
        try {
            s = new Socket("127.0.0.1", 8888);
            System.out.println("连接服务器成功");
            oos = new ObjectOutputStream(s.getOutputStream());
            um = new UserMessage("Check", new User("admin","123456"));
            oos.writeObject(um);
            System.out.println("向服务器发送信息成功");
            ois = new ObjectInputStream(s.getInputStream());
            Object obj = ois.readObject();
            System.out.println("接收到了客户端返回的对象");
            UserMessage um2 = (UserMessage)obj;
            if (um2.getType().equals("success")){
                System.out.println("登陆成功");
            }else{
                System.out.println("登陆失败");
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
